package aos.ydksxt.service.cxfxtj;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.service.common.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * 土地信息查询服务
 * Created by stiofan on 2017/2/2.
 */
@Service
public class TdxxcxfxService extends CommonService {
    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setViewPath("ydksxt/cxfxtj/tdxxcxfx.jsp");
    }
    @Transactional
    public void queryTdxxcxfxByParams(HttpModel httpModel) {
        String querySql = "with tdsyq as\n" +
                " (select nvl(kz.shxydm, ry.nsrsbh) nsrsbh,\n" +
                "         ry.nsrmc,\n" +
                "         ry.lxrxm,\n" +
                "         ry.lxrsfzjlx_dm,\n" +
                "         ry.lxrsfzjhm,\n" +
                "         ry.lxryddh,\n" +
                "         ry.lxrdz_xzqhds_dm,\n" +
                "         ry.lxrdz_xzqhxq_dm,\n" +
                "         ry.lxrdz_jdxz_dm,\n" +
                "         ry.lxrdz_sqjq_dm,\n" +
                "         ry.lxrdz_dlxd_dm,\n" +
                "         ry.lxrdz_lh,\n" +
                "         ry.zgswjg_dm,\n" +
                "         ry.zgswskfj_dm,\n" +
                "         ry.ssgly_dm,\n" +
                "         td.tdbh,\n" +
                "         td.tdzdh,\n" +
                "         td.tdbjsz_d,\n" +
                "         td.tdbjsz_x,\n" +
                "         td.tdbjsz_n,\n" +
                "         td.tdbjsz_b,\n" +
                "         td.xzqhds_dm,\n" +
                "         td.xzqhxq_dm,\n" +
                "         td.jdxz_dm,\n" +
                "         td.zldz_sqjq_dm,\n" +
                "         td.zldz_dlxd_dm,\n" +
                "         td.zldz_lh,\n" +
                "         td.tdxz_dm,\n" +
                "         td.tdyt_dm,\n" +
                "         td.tdzmj,\n" +
                "         syq.tdsyrlx_dm,\n" +
                "         syq.tdsyqlx_dm,\n" +
                "         syq.tdsyq_qsrq,\n" +
                "         syq.tdsyq_zzrq,\n" +
                "         syq.zfr_dm,\n" +
                "         syq.zfrq,\n" +
                "         syq.zfyy,\n" +
                "         syq.tdsyq_zsbh,\n" +
                "         syq.tdsyq_qdrq,\n" +
                "         mx.tddj_dm,\n" +
                "         mx.dwse_dm,\n" +
                "         mx.tdmj,\n" +
                "         mx.ynse,\n" +
                "         mx.tdyz,\n" +
                "         mx.tddjmxuuid,\n" +
                "         syq.tdsyquuid\n" +
                "    from ydks.dj_ryxx          ry,\n" +
                "         ydks.dj_ryxx_kz       kz,\n" +
                "         ydks.dj_tdjcxx        td,\n" +
                "         ydks.dj_tdjcxx_tddjmx mx,\n" +
                "         ydks.dj_tdsyqdjxx     syq\n" +
                "   where ry.djxh = kz.djxh\n" +
                "     and ry.djxh = syq.djxh\n" +
                "     and td.tdbh = syq.tdbh\n" +
                "     and td.tdbh = mx.tdbh\n" +
                "     and ry.yxbz = 'Y'\n" +
                "     and kz.yxbz = 'Y'\n" +
                "     and td.yxbz = 'Y'\n" +
                "     and mx.yxbz = 'Y'\n" +
                "     and syq.yxbz = 'Y'\n" +
                "     and syq.tdsyqlx_dm = '01')\n" +
                "select syq.*,\n" +
                "       syq.tdmj - nvl(jm.jmmj, 0) ysmj,\n" +
                "       syq.ynse - nvl(jm.jmse, 0) ysje,\n" +
                "       jm.tdjmxz_dm,\n" +
                "       jm.jmmj,\n" +
                "       jm.jmse,\n" +
                "       jm.jm_yxqq,\n" +
                "       jm.jm_yxqz\n" +
                "  from tdsyq syq, ydks.dj_tdsyqdjxx_jmxx jm\n" +
                " where syq.tddjmxuuid = jm.tddjmxuuid(+)\n" +
                "   and syq.tdsyquuid = jm.tdsyquuid(+)\n" +
                "   and 'Y' = jm.yxbz(+)\n";
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto();
        if (StringUtils.isNotEmpty(inDto.getString("nsrsbh"))) {
            params.put("nsrsbh", inDto.getString("nsrsbh"));
            querySql += "   and syq.nsrsbh=:nsrsbh\n";
        }
        if (StringUtils.isNotEmpty(inDto.getString("nsrmc"))) {
            params.put("nsrmc", "%"+inDto.getString("nsrmc")+"%");
            querySql += "   and syq.nsrmc like :nsrmc\n";
        }
        if (StringUtils.isNotEmpty(inDto.getString("tdzdh"))) {
            params.put("tdzdh", inDto.getString("tdzdh"));
            querySql += "   and syq.tdzdh=:tdzdh\n";
        }
        if (StringUtils.isNotEmpty(inDto.getString("tdbh"))) {
            params.put("tdbh", inDto.getString("tdbh"));
            querySql += "   and syq.tdbh=:tdbh\n";
        }
      if (StringUtils.isNotEmpty(inDto.getString("ynseq"))) {
            params.put("ynseq", inDto.getString("ynseq"));
            querySql += "   and syq.ynse>=:ynseq\n";
        }
      if (StringUtils.isNotEmpty(inDto.getString("ynsez"))) {
          params.put("ynsez", inDto.getString("ynsez"));
          querySql += "   and syq.ynse<=:ynsez\n";
      }
      if (StringUtils.isNotEmpty(inDto.getString("tdjmxz_dm"))) {
            params.put("tdjmxz_dm", inDto.getString("tdjmxz_dm"));
            querySql += "   and jm.tdjmxz_dm=:tdjmxz_dm\n";
        }
      if (StringUtils.isNotEmpty(inDto.getString("jmseq"))) {
          params.put("jmseq", inDto.getString("jmseq"));
          querySql += "   and jm.jmse>=:jmseq\n";
      }
      if (StringUtils.isNotEmpty(inDto.getString("jmsez"))) {
          params.put("jmsez", inDto.getString("jmsez"));
          querySql += "   and jm.jmse<=:jmsez\n";
      }
        if ("false".equals(inDto.getString("isYz"))) {
            querySql += "   and syq.tdsyq_zsbh is null\n";
        }else if("true".equals(inDto.getString("isYz"))){
            querySql += "   and syq.tdsyq_zsbh is not null\n";
        }
        if (StringUtils.isNotEmpty(inDto.getString("tddjDm"))) {
            params.put("tddjDm", inDto.getString("tddjDm"));
            querySql += "   and syq.tddj_dm=:tddjDm\n";
        }
        if (!StringUtils.isEmpty(inDto.getString("zgswjgDm"))) {
            params.put("zgswjgDm", inDto.get("zgswjgDm"));
            querySql += "   and syq.zgswjg_dm = :zgswjgDm\n";
        }
        if (!StringUtils.isEmpty(inDto.getString("zgswskfjDm"))) {
            params.put("zgswskfjDm", inDto.get("zgswskfjDm"));
            querySql += "   and syq.zgswskfj_dm = :zgswskfjDm\n";
        }
        if (!StringUtils.isEmpty(inDto.getString("ssglyDm"))) {
            params.put("ssglyDm", inDto.get("ssglyDm"));
            querySql += "   and syq.ssgly_dm = :ssglyDm\n";
        }
        querySql += " order by syq.nsrsbh, syq.tdbh, syq.tdsyqlx_dm, syq.tddj_dm, jm.tdjmxz_dm\n";
        List results = persistenceDAO.findAllBySql(querySql, params);
        for (int i = 0; i < results.size(); i++) {
            Map map = (Map) results.get(i);
            map.put("lxrdz", getFullAddress((String) map.get("lxrdzXzqhdsDm"), (String) map.get("lxrdzXzqhxqDm"), (String) map.get("lxrdzJdxzDm"), (String) map.get("lxrdzSqjqDm"), (String) map.get("lxrdzDlxdDm"), (String) map.get("lxrdzLh")));
            map.put("zldz", getFullAddress((String) map.get("xzqhdszDm"), (String) map.get("xzqhxqDm"), (String) map.get("jdxzDm"), (String) map.get("zldzSqjqDm"), (String) map.get("zldzDlxdDm"), (String) map.get("zldzLh")));
        }
        httpModel.setOutMsg(AOSJson.toGridJson(AOSUtils.list2Map(results)));
    }
}
